package com.yangjian.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangjian.bean.Dish;
import com.yangjian.comon.R;
import com.yangjian.dto.DishDto;
import com.yangjian.page.PageParameter;
import com.yangjian.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 */

@RestController
public class DishController {


    @Autowired
    private DishService ds;

    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping("/dish")
    public R add(@RequestBody DishDto dishDto) {
        int row = ds.add(dishDto);
        if (row > 0) {

            return R.success("添加菜品成功");
        } else {

            return R.error("添加菜品失败");
        }
    }

    /**
     * 菜品分页查询（包含数据回显）
     *
     * @param page
     * @return
     */
    @GetMapping("/dish/page")
    public R findPage(PageParameter page) {
        IPage<DishDto> page1 = ds.findPage(page);
        if (page != null) {
            return R.success(page1);
        } else {
            return R.error("分页查询菜单失败");
        }
    }


    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */

    @PutMapping("/dish")
    public R update(@RequestBody DishDto dishDto) {

        int row = ds.update(dishDto);

        if (row > 0) {

            return R.success("修改菜品成功");
        } else {

            return R.error("修改菜品失败");
        }
    }


    /**
     * 根据分类id来查菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/dish/list")
    public R findByCategoryId(long categoryId,Integer status) {

        try {
            List<DishDto>  dishes= ds.findByCategoryId(categoryId, status);
            return R.success(dishes);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }


    @PostMapping("/dish/status/{status}")
    public R updateStatus(@PathVariable("status") int status,@RequestParam List<Long> ids){

        System.out.println("status = " + status);
        System.out.println("ids = " + ids);

        int row = ds.updateStatus(status, ids);

        if (row > 0) {

            return R.success("修改菜品售卖状态成功");
        } else {

            return R.error("修改菜品售卖状态失败");
        }
    }


    @DeleteMapping("/dish")
    public R delete(@RequestParam List<Long> ids){
        int row = ds.delete(ids);
        if (row > 0) {

            return R.success("删除菜品成功");
        } else {

            return R.error("删除菜品失败");
        }
    }

}
